import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import com.RspInterface 1.0
import QtCharts 2.2
import com.ComboData 1.0
import "public.js" as Public


//风险分布图
Item {
    property var tabName
    property var branchID
    property var sectionSpace: 10
    property var kvSpace: 2
    property ComboData branchModel: g_MainQuo.getMyBranchs()
    property var normalCount: 0
    property var callCount: 0
    property var forceCount: 0
    property var forceAbsCount: 0
    property var normalPer
    property var callPer
    property var forcePer
    property var forceAbsPer
    ColumnLayout {
        anchors.fill: parent
        RowLayout {
            anchors.top: parent.top
            anchors.topMargin: 5
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.right: parent.right
            //            anchors.rightMargin: 20
            Layout.preferredHeight: 30
            Layout.alignment: Qt.AlignHCenter
            z: 5

            Text {
                text: "代理商" + colon
                anchors.left: parent.left
            }
            ComboBox {
                id: cmbBranch
                model: branchModel
                Layout.preferredWidth: 160
            }

            Text {
                id: beginT
                text: "开始日期" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.left: cmbBranch.right
                anchors.leftMargin: sectionSpace
            }
            DatePicker {
                id: b_date
                Layout.preferredWidth: 110
                anchors.left: beginT.right
                anchors.leftMargin: kvSpace
            }

            Text {
                id: endT
                text: "结束日期" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.left: b_date.right
                anchors.leftMargin: sectionSpace
            }
            DatePicker {
                id: e_date
                Layout.preferredWidth: 110
                anchors.left: endT.right
                anchors.leftMargin: kvSpace
            }

            Button {
                text: "查询"
                anchors.right: parent.right
                anchors.rightMargin: 20
                onClicked: {
                    var bDate = b_date.getDateStr()
                    var eDate = e_date.getDateStr()
                    if (parseInt(bDate) > parseInt(eDate)) {
                        tipInput.text = "开始日期不能大于结束日期!"
                        tipInput.open()
                        return
                    }
                    var req = "#branch_id=" + branchModel.get(
                                cmbBranch.currentIndex)
                    req += "#begin_date=" + b_date.getDateStr()
                    req += "#end_date=" + e_date.getDateStr()
                    var rsp = g_MainQuo.queryRiskCustCount(req)

                    custDistrModel.clear()
                    var almName = "risk_dist"
                    var rowCount = rsp.getAlmSize(almName)
                    for (var i = 0; i < rowCount; ++i) {
                        var lv1Count = parseInt(rsp.getAlmString(almName, i, 2))
                        var lv2Count = parseInt(rsp.getAlmString(almName, i, 4))
                        var lv3Count = parseInt(rsp.getAlmString(almName, i, 6))
                        var lv4Count = parseInt(rsp.getAlmString(almName, i, 8))
                        var total = lv1Count + lv2Count + lv3Count + lv4Count
                        custDistrModel.append({
                                                  Date: rsp.getAlmString(
                                                            almName, i, 0),
                                                  LV1Count: lv1Count,
                                                  LV1Percent: Public.getPercent(
                                                                  lv1Count,
                                                                  total),
                                                  LV2Count: lv2Count,
                                                  LV2Percent: Public.getPercent(
                                                                  lv2Count,
                                                                  total),
                                                  LV3Count: lv3Count,
                                                  LV3Percent: Public.getPercent(
                                                                  lv3Count,
                                                                  total),
                                                  LV4Count: lv4Count,
                                                  LV4Percent: Public.getPercent(
                                                                  lv4Count,
                                                                  total),
                                                  TotalCount: total
                                              })
                    }
                }
            }
        }

        SplitView {
            anchors.top: parent.top
            anchors.topMargin: 40
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 40
            //            Layout.fillHeight: true
            TableView {
                width: parent.width * 0.7
                Layout.minimumWidth: 250
                model: custDistrModel
                TableViewColumn {
                    title: "日期"
                    role: "Date"
                    resizable: true
                    width: parent.width * 0.1
                }

                TableViewColumn {
                    title: "正常客户数"
                    role: "LV1Count"
                    resizable: true
                    width: parent.width * 0.1
                }
                TableViewColumn {
                    title: "正常客户百分比"
                    role: "LV1Percent"
                    resizable: true
                    width: parent.width * 0.1
                }
                TableViewColumn {
                    title: "追保客户数"
                    role: "LV2Count"
                    resizable: true
                    width: parent.width * 0.1
                }
                TableViewColumn {
                    title: "追保客户百分比"
                    role: "LV2Percent"
                    resizable: true
                    width: parent.width * 0.1
                }
                TableViewColumn {
                    title: "相对强平客户数"
                    role: "LV3Count"
                    resizable: true
                    width: parent.width * 0.1
                }
                TableViewColumn {
                    title: "相对强平客户百分比"
                    role: "LV3Percent"
                    resizable: true
                    width: parent.width * 0.1
                }
                TableViewColumn {
                    title: "绝对强平客户数"
                    role: "LV4Count"
                    resizable: true
                    width: parent.width * 0.1
                }
                TableViewColumn {
                    title: "绝对强平客户百分比"
                    role: "LV4Percent"
                    resizable: true
                    width: parent.width * 0.1
                }
                TableViewColumn {
                    title: "总客户数"
                    role: "TotalCount"
                    resizable: true
                    width: parent.width * 0.1
                }

                onDoubleClicked: {
                    normalCount = custDistrModel.get(row).LV1Count
                    callCount = custDistrModel.get(row).LV2Count
                    forceCount = custDistrModel.get(row).LV3Count
                    forceAbsCount = custDistrModel.get(row).LV4Count
                    normalPer = custDistrModel.get(row).LV1Percent
                    callPer = custDistrModel.get(row).LV2Percent
                    forcePer = custDistrModel.get(row).LV3Percent
                    forceAbsPer = custDistrModel.get(row).LV4Percent
                }
            }

            ChartView {
                Layout.minimumWidth: 50
                Layout.fillWidth: true
                theme: ChartView.ChartThemeBrownSand
                antialiasing: true
                visible: (forceAbsCount + normalCount + callCount + forceCount) == 0 ? false : true

                PieSeries {
                    id: pieSeries
                    PieSlice {
                        label: qsTr("绝对强平") + forceAbsPer
                        value: forceAbsCount
                        color: "red"
                        labelVisible: forceAbsCount != 0 ? true : false
                    }
                    PieSlice {
                        label: qsTr("正常") + normalPer
                        value: normalCount
                        color: "green"
                        labelVisible: normalCount != 0 ? true : false
                    }
                    PieSlice {
                        label: qsTr("追保") + callPer
                        value: callCount
                        color: "blue"
                        labelVisible: callCount != 0 ? true : false
                    }
                    PieSlice {
                        label: qsTr("相对强平") + forcePer
                        value: forceCount
                        color: "yellow"
                        labelVisible: forceCount != 0 ? true : false
                    }
                }
            }
        }
    }

    Button {
        text: "关闭"
        anchors.right: parent.right
        anchors.rightMargin: 25
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        onClicked: closeTab(tabName)
    }

    ListModel {
        id: custDistrModel
    }
}
